package cn.bdqn.service.impl;

import cn.bdqn.domain.Routes;
import cn.bdqn.mapper.RoutesMapper;
import cn.bdqn.service.RoutesService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * 路线业务实现类
 * @author shaojie
 */
@Service
public class RoutesServiceImpl implements RoutesService {

    @Autowired
    private RoutesMapper routesMapper;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    //------------------------------服务熔断---------------------------
    // 在10秒内，如果10(或者以上)次请求有6次是失败的，就会进行服务熔断
    @HystrixCommand(fallbackMethod = "getInfo_timeoutHandler",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),// 是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),// 请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "20000"), // 时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")// 失败率达到多少后跳闸
    })
    @Override
    public Routes queryRoutesByPrimaryKey(Integer id) {
        return routesMapper.selectByPrimaryKey(id);
    }

    /**
     * 选择更新
     * @param record
     */
    //------------------------------服务熔断---------------------------
    // 在10秒内，如果10(或者以上)次请求有6次是失败的，就会进行服务熔断
    @HystrixCommand(fallbackMethod = "getInfo_timeoutHandler",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),// 是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),// 请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "20000"), // 时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")// 失败率达到多少后跳闸
    })
    @Override
    public void updateByPrimaryKeySelective(Routes record) {
            routesMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 多条件查询   分页
     * @param routes
     * @return
     */
    //------------------------------服务熔断---------------------------
    // 在10秒内，如果10(或者以上)次请求有6次是失败的，就会进行服务熔断
    @HystrixCommand(fallbackMethod = "getInfo_timeoutHandler",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),// 是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),// 请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "20000"), // 时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")// 失败率达到多少后跳闸
    })
    @Override
    public Page queryByVehicle(Routes routes,Integer pageNum,Integer pageAmount){

        // 开始分页
        PageHelper.startPage(pageNum>0?pageNum:1,pageAmount);

        // 获取路线信息
        List<Routes> routesList = routesMapper.selectByVehicle(routes);

        // 分页
        PageInfo<Routes> routesPageInfo = new PageInfo<>(routesList);

        // 封装分页信息
        Page page = new Page();

        page.setMenuList(routesPageInfo.getList());

        page.setTotalRecord((int) routesPageInfo.getTotal());

        return page;
    }

    // 兜底的方法
    public Result getInfo_timeoutHandler(@PathVariable("id") Integer id){
        return ResultGenerator.genFailResult("我哭了，我是打杂的");
    }

}
